package com.qfedu.provider.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qfedu.config.RocketmqConfig;
import com.qfedu.provider.entity.DbCollection;
import com.qfedu.provider.mapper.DbCollectionMapper;
import com.qfedu.params.AddCollection;
import com.qfedu.provider.service.DbCollectionService;
import com.qfedu.utils.R;
import lombok.RequiredArgsConstructor;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * @author Administrator
 * @description 针对表【db_collection(收藏)】的数据库操作Service实现
 * @createDate 2024-11-23 11:16:39
 */
@Service
@RequiredArgsConstructor
public class DbCollectionServiceImpl extends ServiceImpl<DbCollectionMapper, DbCollection>
        implements DbCollectionService{
    private final DbCollectionMapper dbCollectionMapper;
    private final RocketMQTemplate rocketMQTemplate;


    @Override
    public R findAll(Integer uId,Integer productionTypeId) {
        //1参数校验
        if (uId==null||productionTypeId==null){
            return R.error("参数不能为空");

        }
        LambdaQueryWrapper<DbCollection> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DbCollection::getUserId,uId);
        wrapper.eq(DbCollection::getStatus,1);
        wrapper.eq(DbCollection::getProductionTypeId,productionTypeId);
        return R.ok(dbCollectionMapper.selectList(wrapper));
    }

    @Transactional
    @Override
    public R Add(AddCollection addCollection,Integer uId) {
        //参数校验
        if (addCollection==null||uId==null){
            return R.error("参数不能为空");
        }
        //判断我的收藏表中是否存在该数据
        LambdaQueryWrapper<DbCollection> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DbCollection::getUserId,uId).
                eq(DbCollection::getProductionId,addCollection.getProductionId()).
                eq(DbCollection::getProductionTypeId,addCollection.getProductionTypeId());
        DbCollection dbCollection = dbCollectionMapper.selectOne(wrapper);
        if (dbCollection!=null){
            //说明已经添加过了，此时需要修改收藏状态
            //i:记录未修改前，收藏的状态
            int i = dbCollection.getStatus();
            dbCollection.setId(dbCollection.getId());
            dbCollection.setStatus(dbCollection.getStatus()==1?0:1);
            dbCollection.setUpdateTime(new Date());
            if (dbCollectionMapper.updateById(dbCollection)<0){
                return R.error("修改我的收藏失败");
            }
            //修改成功，异步调用MQ,修改作品总的收藏数量
            if(i == 1){
                //说明需要为作品的总收藏数-1
                rocketMQTemplate.convertAndSend(RocketmqConfig.TOPIC_COLLECTION_NUM_RED,addCollection);
            }else {
                //说明需要为作品的总收藏数+1
                rocketMQTemplate.convertAndSend(RocketmqConfig.TOPIC_COLLECTION_NUM_ADD,addCollection);
            }
            return R.ok("修改我的收藏成功");
        }
        //没有添加过，做添加操作
        DbCollection dbCollection1 = new DbCollection();
        dbCollection1.setUserId(uId);
        dbCollection1.setProductionId(addCollection.getProductionId());
        dbCollection1.setProductionTypeId(addCollection.getProductionTypeId());
        dbCollection1.setStatus(1);
        dbCollection1.setCreateTime(new Date());
        dbCollection1.setUpdateTime(new Date());
        if (dbCollectionMapper.insert(dbCollection1)<0){
            return R.error("添加到我的收藏失败");
        }
        //添加成功，异步调用MQ,作品总收藏数量+1
        rocketMQTemplate.convertAndSend(RocketmqConfig.TOPIC_COLLECTION_NUM_ADD,addCollection);
        return R.ok("添加到我的收藏成功");
    }


}




